using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MQRevo
{
    /// <summary>
    /// Performs logging functions
    /// </summary>
    internal static class Log
    {
        /// <summary>
        /// Writes msg to log file, folders are created for each day
        /// </summary>
        /// <param name="type">Message type: Error, exception, method invoke etc.</param>
        /// <param name="msg">Message to write to the log file</param>
        /// <param name="stacktrace">Error stack trace</param>
        internal static void WriteLog(string type,string msg,string stacktrace)
        {
            string appDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //check if directory exists and create if it does not
            if (!Directory.Exists( appDir+"\\Log"))
                Directory.CreateDirectory(appDir + "\\Log");
            //check the file
            if (!File.Exists(appDir + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"))
            {
                File.Create(appDir + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
            }
            //check the file
            FileStream fs= new FileStream(appDir + "\\Log\\"+DateTime.Now.ToString("yyyyMMdd")+".txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("Message Type: " + type + System.Environment.NewLine);
            sw.Write("Date/Time: " + DateTime.Now.ToString() + System.Environment.NewLine);
            sw.Write("Message: " + msg + System.Environment.NewLine);
            if (type.ToUpper()=="EXCEPTION")
                sw.Write("StackTrace: " + stacktrace + System.Environment.NewLine);
            sw.Write("================================================" + System.Environment.NewLine);
            sw.Close();
            fs.Close();

        }
        internal static void WriteLog(string type, string msg)
        {
            string appDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //check if directory exists and create if it does not
            if (!Directory.Exists(appDir + "\\Log"))
                Directory.CreateDirectory(appDir + "\\Log");

            //check the file
            //if (!File.Exists(appDir + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"))
            //{
            //    File.Create(appDir + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                
            //}
            FileStream fs = new FileStream(appDir + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("Message Type: " + type + System.Environment.NewLine);
            sw.Write("Date/Time: " + DateTime.Now.ToString() + System.Environment.NewLine);
            sw.Write("Message: " + msg + System.Environment.NewLine);
            sw.Write("================================================" + System.Environment.NewLine);
            sw.Close();
            fs.Close();

        }
    }
}
